﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Reflection;
/*
 * Author:W
 * CSV文本读取解析工具
 */
namespace W.GameFrameWork.ExcelTool
{
	public class CSVHelper
	{
		/// <summary>
        /// 从CSV文件中读取解析
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="filePath"></param>
        /// <returns></returns>
		public static List<T> ReadCSVFile<T>(string filePath)
		{
			FileStream fileStream = File.OpenRead(filePath);
			StreamReader streamReader = new StreamReader(fileStream);

			List<T> objList = new List<T>();

			//获取字段名称
			string fieldStr = streamReader.ReadLine();
			string[] fieldNames = fieldStr.Split(',');

			System.Type classType = typeof(T);
			Assembly assembly = Assembly.GetAssembly(classType);//获取泛型的程序集
			List<FieldInfo> fieldInfos = new List<FieldInfo>(classType.GetFields());//获取到泛型所有字段的集合

			string lineStr;
			string[] valueArr;
			while ((lineStr = streamReader.ReadLine())!= null)
			{
				valueArr = lineStr.Split(',');

				object obj = assembly.CreateInstance(classType.FullName); //泛型实例化

				for (int i = 0; i < fieldNames.Length; i++)
				{
					FieldInfo fieldInfo = fieldInfos.Find(t => t.Name.Equals(fieldNames[i]));
					if (fieldInfo != null)
					{
						//Debug.Log("propertyInfo Name="+propertyInfo.Name+" Type="+propertyInfo.PropertyType);

						if (fieldInfo.FieldType.Equals(typeof(System.Int32[])))
						{
							int[] intArr = GetIntArr(valueArr[i]);
							fieldInfo.SetValue((T)obj, intArr);
						}
						else if (fieldInfo.FieldType.Equals(typeof(System.String[])))
						{
							string[] strArr = valueArr[i].Split(';');
							fieldInfo.SetValue((T)obj, strArr);
						}
						else if (fieldInfo.FieldType.Equals(typeof(System.Single[])))
						{
							float[] floatArr = GetFloatArr(valueArr[i]);
							fieldInfo.SetValue((T)obj, floatArr);

						}else if (fieldInfo.FieldType.Equals(typeof(int)))
						{
							fieldInfo.SetValue((T)obj, int.Parse(valueArr[i]));
						}
						else if (fieldInfo.FieldType.Equals(typeof(string)))
						{
							fieldInfo.SetValue((T)obj, valueArr[i].ToString());
						}
						else if (fieldInfo.FieldType.Equals(typeof(float)))
						{
							fieldInfo.SetValue((T)obj, float.Parse(valueArr[i]));
						}

					}
				}
				
				objList.Add((T)obj);
			}

			streamReader.Close();
			streamReader.Dispose();
			fileStream.Close();
			fileStream.Dispose();

			return objList;
		}

		/// <summary>
		/// 将分割字符串转换成特定数据类型数组
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="type"></param>
		/// <returns></returns>
		private static int[] GetIntArr(string arrStr)
		{
			string[] arr = arrStr.Split(';');
			int[] resArr = new int[arr.Length];
			for (int i = 0; i < arr.Length; i++)
			{
				resArr[i] = int.Parse(arr[i]);
			}
			return resArr;
		}


		/// <summary>
		/// 将分割字符串转换成特定数据类型数组
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="type"></param>
		/// <returns></returns>
		private static float[] GetFloatArr(string arrStr)
		{
			string[] arr = arrStr.Split(';');
			float[] resArr = new float[arr.Length];
			for (int i = 0; i < arr.Length; i++)
			{
				resArr[i] = float.Parse(arr[i]);
			}
			return resArr;
		}
	}
}

